// pages/my/my.js
var app = getApp(), api=require("../../api.js");
Page({
    /**
     * 页面的初始数据
     */
    data: {
      isShow: true,
      imgUrls:[
        '../../image/1.jpg',
        '../../image/2.jpg',
        '../../image/3.jpg'
      ],
      qrCode:'',
      unit_price:0.00,
      adv_img:'',
      ad_url:'',
      is_send:false,
      is_waiting:false, 
      equipment:{
        hospital_name:'',
        ward_name:'',
        bed_num:'',
        equipment_num:''
      }
    },
    go_url:function(e){
      var _this = this, url = e.currentTarget.dataset.url;
      url&&wx.navigateTo({
        url: url,
      })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
      var _this=this;
      _this.query();
      //隐藏
      
      if (!wx.getStorageSync('access_token')){
        wx.hideTabBar({})
        _this.setData({ isShow: true });
      }
     
      //console.log(app.globalData.equipment_id);
      app.globalData.equipment_id&&app.request({
        url:api.query_equipment,
        data:{
          equipment_id: app.globalData.equipment_id
        },
        method:'post',
        success:function(res){
         // console.log(res);
            _this.setData({
              equipment:res.data
            })
        }
      })
    },
  query: function () {
    var _this = this;
    if (wx.getStorageSync("access_token")) {
      app.request({
        url: api.query,
        method: 'post',
        success: function (res) {
          if (res.code == -2) {
            console.log(res.data)
            _this.setData({
              equipment:{
                hospital_name: res.data.hospitalname,
                ward_name: res.data.inpatientareaname,
                bed_num: res.data.bednum,
                equipment_num: res.data.equipmentnum,
              }
            })
            wx.navigateTo({
              url: '/pages/order/orderDetail/orderDetail?order_no=' + res.data.order_no,
            })

          }
        }
      })
    }
  },
  details: function (e) {

    var title = e.currentTarget.dataset.title, genre = e.currentTarget.dataset.genre;
    wx.navigateTo({
      url: '../details/details?title=' + title + '&genre=' + genre,
    })
  },
    // 点击广告
    clickAdv:function(info){
      var _this = this;
      if (!wx.getStorageSync('access_token')){
        if (!info.detail || !info.detail.encryptedData || !info.detail.iv) {
          wx.showModal({
            title: '提示',
            content: '为了你更好的体验请点击同意',
            showCancel: false
          })
          return false;
        }
        wx.showLoading({
          title: '请稍后',
        })
        wx.login({
          success: (res) => {
            app.request({
              url: api.login,
              data: {
                code: res.code,
                encryptedData: info.detail.encryptedData,
                iv: info.detail.iv
              },
              method: 'post',
              success: function (res) {
                wx.setStorageSync('access_token', res.data.access_token);
        
                //展示底部标签
                wx.showTabBar();
                // console.log(" wx.showTabBar()")
                _this.setData({
                  isShow: false
                })
                wx.hideLoading()
                _this.data.ad_url&&wx.navigateTo({
                  url: _this.data.ad_url,
                })

              }
            })

          }
        })
      }else{
        _this.setData({
          isShow: false
        })
        wx.hideLoading()
        _this.data.ad_url && wx.navigateTo({
          url: _this.data.ad_url,
        })
      } 
    },
  getUserInfo:function(info){
    var _this=this;
    if (!wx.getStorageSync('access_token')){
    if (!info.detail || !info.detail.encryptedData || !info.detail.iv){
     wx.showModal({
       title: '提示',
       content: '为了你更好的体验请点击同意',
       showCancel:false
     })
     return false;
    }
    wx.showLoading({
      title: '请稍后',
    })
      wx.login({
        success:(res)=>{
         app.request({
           url: api.login,
           data:{
             code:res.code,
             encryptedData: info.detail.encryptedData,
             iv: info.detail.iv
           },
           method:'post',
           success:function(res){
             wx.showTabBar();
             _this.setData({
               isShow: false
             })
             wx.setStorageSync('access_token', res.data.access_token) ;
             _this.query()
             wx.hideLoading()
           }
         })
          
        }
      })
    }else{
      _this.setData({
        isShow: false
      })
      wx.hideLoading()
    }
   
    },
    goAgree:function(){
      wx.navigateTo({
        url: './agree/law/use/use',
      })
    },
  clickScan:function(){
    var _this=this;
    if (app.globalData.equipment_id){
      wx.showLoading({
        title: '请稍后',
      })
       app.request({
         url: api.send_equipment,
         data:{
           equipment_id: app.globalData.equipment_id
         },
         method:'post',
         success:function(res){
           wx.hideLoading();
           app.globalData.equipment_id = null;
           if(res.code===0){
          
             wx.showToast({
               title: res.msg,
               duration:1000,
               success:function(){
                 setTimeout(function(){
                   wx.navigateTo({
                     url: '/pages/order/orderDetail/orderDetail?order_no=' + res.data.order_no,
                   })
                 },1000)
               }
             })
           }
           if(res.code===-2){
       
             wx.showToast({
               title: res.msg,
               icon:'none'
             })
           }
           if (res.code === -3) {
        
             wx.navigateTo({
               url: '/pages/order/orderDetail/orderDetail?order_no='+res.data.order_no,
             })
           }
         }
       })
    }else{
        _this.scanCode();
    }
  },
  scanCode:function(){
    var _this = this;
    wx.scanCode({
      success: (res) => {
        if (!res.result) {
          wx.showToast({
            title: '识别二维码失败请重试',
            icon: 'none',
            duration: 2000
          })
          return false;
        }
        wx.showLoading({
          title: '识别中',
        })
        var id = _this.getRequest(encodeURIComponent(res.result));
        app.request({
          url: api.query_equipment,
          data: {
            equipment_id: id
          },
          method: 'post',
          success: function (res) {
            app.globalData.equipment_id = id;
            _this.setData({
              equipment: res.data
            })
            wx.hideLoading();
          }
        })
      },
      fail: (res) => {
        wx.showToast({
          title: '扫描失败',
          icon: 'none',
          duration: 2000
        })
      },
      complete: (res) => {
      }
    })  
  },
  getRequest: function (url) {
    if (!url)
      return
    let id;
    if (url.indexOf("") != -1) {
      id = url.split('%3F')[1].split('%3D')[1];

    }
    return id;
  },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
    
    },
  
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
      var _this = this;
      
        app.request({
          url:api.index,
          method:'post',
          success:function(res){
            _this.setData({
              imgUrls:res.data.banner,
              unit_price: res.data.unit_price,
              adv_img: res.data.adv_img,
              ad_url:res.data.ad_url
            })
          }
        })
    },
  
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {
    
    },
  
    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {
    
    },
  
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
    
    },
  
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
    
    },
  
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
    
    }
  })